package diseaseandcureadministration;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;

import diseaseandcureadministration.registry.Registrable;

/**
 * AbstractLoader defines concrete Loader classes
 * 
 * @author Peter Tolstrup Aagesen, ptaa@itu.dk
 *
 */
public abstract class AbstractLoader 
{	
	//Instance variables
	private String _fileName;
	
	/**
	 * setFileName is a setter method for setting
	 * the file variable
	 * 
	 * @param fileName name of file to load from
	 */
	public void setFileName(String fileName)
	{
		_fileName = fileName;
	}
	
	/**
	 * getFileName is a getter method for file variable
	 * 
	 * @return fileName 
	 */
	public String getFileName()
	{
		return _fileName;
	}
	
	/**
	 * load method is the method for loading and creating 
	 * elements 
	 * load must be implemented by concrete loader classes
	 * 
	 * @return list of elements
	 * @throws FileNotFoundException
	 */
	public abstract ArrayList<Registrable> load() throws FileNotFoundException;
	
	/**
	 * save writes the elements to a file 
	 * 
	 * @param elements
	 * @throws FileNotFoundException
	 */
	public void save(ArrayList<Registrable> elements) throws FileNotFoundException
	{
		//Create variables
		PrintWriter fileWriter = new PrintWriter(_fileName);
		String writerLine = "";
		
		try
		{
			//Traverse elements
			for(Registrable element: elements)
			{
				//Write the to file
				writerLine = element.toString();
				fileWriter.println(writerLine);
			}
			
		}
		//Ensures the PrintWriter is closed
		finally
		{		
			fileWriter.close();			
		}
	}

}
